#Policy views by racial group
#Mac Lockhart
#December 15, 2023

library(data.table)
library(tidyverse)
library(ggplot2)

policy <- fread("Data/policy_outcomes_withissueareas_AM25.csv")

policy$bill_status <- ifelse(policy$became_law==1, "Law",
                             ifelse(policy$passed_senate == 1, "Passed Senate",
                                    ifelse(policy$considered_senate==1, "Considered Senate",
                                           ifelse(policy$passed_house==1, "Considered House",
                                                  ifelse(policy$considered_house==1, "Considered House", "Not Considered")))))
  
race_support <- policy %>% group_by(year_vote, race, year, bill_status) %>% summarize(support = mean(outcome==1, na.rm=T)) %>%ungroup()
race_support <- race_support %>% pivot_wider(names_from = race, values_from = support)
colnames(race_support)<-c("year_vote", "year", "bill_status","white", "black", "hispanic",
                          "asian", "native_american", "mixed",
                          "other", "middle_eastern", "missing")
race_support <- race_support%>%arrange(desc(white))

race_support$black_gap <- race_support$black-race_support$white
race_support$latino_gap <- race_support$hispanic-race_support$white
race_support$asian_gap <- race_support$asian-race_support$white

par(mfrow=c(2,2))
hist(race_support$black_gap, main="Black Opinion Gaps", xlab = "Black support minus White support")
hist(race_support$latino_gap, main="Latino Opinion Gaps", xlab = "Latino support minus White support")
hist(race_support$asian_gap, main="Asian Opinion Gaps", xlab = "Asian support minus White support")
dev.off()

plot(race_support$year, race_support$black_gap)

out <- select(race_support, c("year_vote", "black_gap", "latino_gap", "asian_gap", "bill_status"))

max_blackgap <- race_support %>% arrange(desc(abs(black_gap))) %>% head(10)
min_blackgap <- race_support %>% arrange(abs(black_gap)) %>% head(10)
max_latinogap <- race_support %>% arrange(desc(abs(latino_gap))) %>% head(10)
min_latinogap <- race_support %>% arrange(abs(latino_gap)) %>% head(10)



cap_codes<-fread("Intermediate Data/Congressional Agenda/CES CAP Coding All Questions.csv")

cap_codes$year_vote <- paste0(cap_codes$data_year,"_rcv",cap_codes$data_num, "_r")
race_support<-left_join(race_support, cap_codes, b=c("year_vote" = "year_vote"))
race_support_out <- race_support[,c("year_vote", "bill_status",
                                    "white", "black_gap", "latino_gap", "asian_gap",
                                    "cces_exact_wording", "Bill_topic")]
fwrite(race_support_out, "Data/Racial_opinion_gaps.csv")
#create table for appendix
race_support_out <- race_support[,c("year.x", "bill_status",
                                    "white", "black", "hispanic", "asian",
                                    "cces_exact_wording", "Bill_topic")]
race_support_out<-race_support_out %>% mutate_if(is.numeric, ~round(., 3))

fwrite(race_support_out, "Data/Racial_opinion_gaps_appendix.csv")
rm(cap_codes)
